﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ResourceSystem
{
    public class ResFileUtils
    {
        /// <summary>
        /// 获得文件MD5
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string GetFileMD5(string file)
        {
            FileStream fs = new FileStream(file, FileMode.Open);

            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(fs);
            fs.Close();

            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < retVal.Length; j++)
            {
                sb.Append(retVal[j].ToString("x2"));
            }
            return sb.ToString();
        }


        /// <summary>
        /// 处理文件夹是否存在，不存在就创建一个，存在就删掉重新建一个
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static void PrecessDirectoryExist(string dirPath)
        {
            DirectoryInfo info=new DirectoryInfo(dirPath);
            if (info.Exists)
            {
               info.Delete(true);
            }
            Directory.CreateDirectory(dirPath);
        }

        /// <summary>
        /// 通过文件扩展名筛选文件
        /// </summary>
        /// <param name="files"></param>
        /// <param name="filters"></param>
        /// <returns></returns>
        public static List<string> SelectFilesByFileFilter(string[] files,string[] filters)
        {
            
            List<string> filePaths = new List<string>();
            bool isTargetFileExtension;
            for (int i = 0; i < files.Length; i++)
            {
                isTargetFileExtension = true;
                for (int k = 0; k < filters.Length; k++)
                {
                    string extension = PathUtils.GetExtension(files[i]);
                    if (extension == filters[k])
                    {
                        isTargetFileExtension = false;
                    }
                }
                if (isTargetFileExtension)
                {
                    filePaths.Add(files[i]);
                }
            }
            return filePaths;
        }

        /// <summary>
        /// 通过文件扩展名筛选文件
        /// </summary>
        /// <param name="files"></param>
        /// <param name="filters"></param>
        /// <returns></returns>
        public static List<string> SelectFilesByFileFilter(FileInfo[] files, string[] filters)
        {

            List<string> filePaths = new List<string>();
            bool isTargetFileExtension;
            for (int i = 0; i < files.Length; i++)
            {
                isTargetFileExtension = true;
                for (int k = 0; k < filters.Length; k++)
                {
                    string extension = files[i].Extension;
                    if (extension == filters[k])
                    {
                        isTargetFileExtension = false;
                    }
                }
                if (isTargetFileExtension)
                {
                    filePaths.Add(files[i].FullName);
                }
            }
            return filePaths;
        }

        /// <summary>
        /// 获取在某些目录下的文件
        /// </summary>
        /// <param name="filePaths"></param>
        /// <param name="dirFilters"></param>
        /// <returns></returns>
        public static List<string> SelectFilesByDirectoryPath(string[] filePaths,string[] dirFilters)
        {
            List<string> returnPaths = new List<string>();
            for (int i = 0; i < filePaths.Length; i++)
            {
                for (int j = 0; j < dirFilters.Length; j++)
                {
                    if (filePaths[i].Contains(dirFilters[j]))
                    {
                        returnPaths.Add(filePaths[i]);
                    }
                }

            }
            return returnPaths;
        }

        /// <summary>
        /// 获取不在某些目录下的文件
        /// </summary>
        /// <param name="filePaths"></param>
        /// <param name="dirFilters"></param>
        /// <returns></returns>
        public static List<string> SelectFilesExceptDirectoryPath(string[] filePaths, string[] dirFilters)
        {
            List<string> returnPaths = new List<string>();
            for (int i = 0; i < filePaths.Length; i++)
            {
                for (int j = 0; j < dirFilters.Length; j++)
                {
                    if (!filePaths[i].Contains(dirFilters[j]))
                    {
                        returnPaths.Add(filePaths[i]);
                    }
                }

            }
            return returnPaths;
        }
    }
}
